﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using flocking.animal;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using flocking.behavior;

namespace flocking {
    public class AnimalUpdater {
        public Formation Formation { get; set; }
        private bool leftButtonPressed;
        BehaviorDisplay display;
        Dictionary<Keys, bool> keyMap;
        LinkedList<Keys> keyFired;

        public AnimalUpdater(BehaviorDisplay _disp) {
            leftButtonPressed = false;
            display = _disp;
            display.listeners.Add(new DisplayChanged(displayChanged));
            keyMap = new Dictionary<Keys,bool>();
            keyFired = new LinkedList<Keys>();
        }

        public void displayChanged(BehaviorDisplay disp) {
        }
        public void update(Game game, GameTime gametime) {
            IEnumerable<Animal> list = Formation.AnimalList;
            float dt = (float)gametime.ElapsedGameTime.TotalSeconds;
            foreach (Animal anm in list) {
                anm.update(dt, Formation);
            }
        }
        public void checkInput(GraphicsDeviceManager graphics, Scene scene) {
            checkCursor();
            checkClick(graphics, scene);
        }
        public void checkCursor() {
            KeyboardState kstate = Keyboard.GetState();
            Keys[] keys = new Keys[] { Keys.Right, Keys.Left, Keys.Up, Keys.Down, Keys.Enter };
            foreach (Keys k in keys) {
                if (kstate.IsKeyDown(k))
                    keyMap[k] = true;
                else if (kstate.IsKeyUp(k) && keyMap.ContainsKey(k) && keyMap[k]) {
                    keyMap[k] = false;
                    keyFired.AddLast(k);
                }
            }
            foreach (Keys k in keyFired) {
                List<Behavior> behavs = display.getCurrentAnimal();
                switch (k) {
                    case Keys.Up:
                        display.index++;
                        if (display.index >= behavs.Count)
                            display.index = 0;
                        break;
                    case Keys.Down:
                        display.index--;
                        if (display.index < 0)
                            display.index = behavs.Count - 1;
                        break;
                    case Keys.Right:
                        display.animal++;
                        display.index = 0;
                        if (display.animal >= display.behavList.Count)
                            display.animal = 0;
                        break;
                    case Keys.Left:
                        display.animal--;
                        display.index = 0;
                        if (display.animal < 0)
                            display.animal = display.behavList.Count - 1;
                        break;
                    case Keys.Enter:
                        Behavior bhv = behavs[display.index];
                        bhv.Toggle = (bhv.Toggle ? false : true);
                        break;
                }
            }
            if (keyFired.Count > 0) {
                keyFired.Clear();
                display.notify();
            }

        }
        public void checkClick(GraphicsDeviceManager graphics, Scene scene) {
            MouseState mstate = Mouse.GetState();
            if (0 <= mstate.X && mstate.X < graphics.PreferredBackBufferWidth
                && 0 <= mstate.Y && mstate.Y < graphics.PreferredBackBufferHeight) {
                switch (mstate.LeftButton) {
                    case ButtonState.Pressed:
                        leftButtonPressed = true;
                        break;
                    case ButtonState.Released:
                        if (leftButtonPressed == false)
                            break;
                        scene.reset();
                        leftButtonPressed = false;
                        break;
                }
            }

        }
    }
}
